﻿using System.Reflection;

namespace NET6.WebAPI.Extensions
{
    public static class ReflectionExtension
    {
        /// <summary>
        /// 获取字段信息
        /// </summary>
        /// <param name="valueType"></param>
        /// <returns></returns>
        public static MemberInfo GetFieldInfo(this ValueType valueType)
        {
            return valueType.GetType().GetField(valueType.ToString());
        }

        /// <summary>  
        /// 获取特性值  
        /// </summary>  
        /// <typeparam name="TAttribute">特性类型</typeparam>  
        /// <typeparam name="TReturn">返回值类型</typeparam>  
        /// <param name="memberInfo"></param>  
        /// <param name="valueSelector">返回字段选择委托</param>  
        /// <returns></returns>  
        public static TReturn GetAttributeValue<TAttribute, TReturn>(
            this MemberInfo memberInfo, Func<TAttribute, TReturn> valueSelector) where TAttribute : Attribute
        {
            var attr = memberInfo.GetCustomAttribute<TAttribute>();
            return attr != null ? valueSelector(attr) : default;
        }
    }
}
